package com.zzq.my.jetpacks.jectpackapp.ui.dzfs

import android.view.View
import by.kirich1409.viewbindingdelegate.viewBinding
import com.aisier.architecture.base.BaseFragment
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener
import com.wuyr.activitymessenger.startActivity
import com.zzq.my.jetpacks.jectpackapp.R
import com.zzq.my.jetpacks.jectpackapp.adapter.AccountInRecordGroupAdapter
import com.zzq.my.jetpacks.jectpackapp.adapter.AccountInRecordGroupAdapter2
import com.zzq.my.jetpacks.jectpackapp.bean.AccountInRecordInfo
import com.zzq.my.jetpacks.jectpackapp.bean.AccountInRecordInfo2
import com.zzq.my.jetpacks.jectpackapp.databinding.ShopIncomingFragmentAccountInRecordBinding
import com.zzq.my.jetpacks.util.showMsg


/**
 * 描述：入账记录的Fragment
 * 作者：zhuangzeqin
 * 时间: 2022/9/11-13:28
 * 邮箱：zzq@eeepay.cn
 * 备注:
 */
class AccountInRecordFragment : BaseFragment(R.layout.shop_incoming_fragment_account_in_record) {
    private val vb by viewBinding(ShopIncomingFragmentAccountInRecordBinding::bind)

    private var pageNo = 1//页码数
    private lateinit var mAccountInRecordGroupAdapter: AccountInRecordGroupAdapter
    private lateinit var mAccountInRecordGroupAdapter2: AccountInRecordGroupAdapter2
    private lateinit var noDataView: View

    companion object {
        fun newInstance() = AccountInRecordFragment()//todo 无惨的实例
    }

    override fun initView() {
        super.initView()
        noDataView =
            layoutInflater.inflate(R.layout.shop_incoming_item_no_data, vb.rvRecordList, false)
        mAccountInRecordGroupAdapter = AccountInRecordGroupAdapter(requireContext())
        mAccountInRecordGroupAdapter2 = AccountInRecordGroupAdapter2(mutableListOf())
        val itemData = getAccountRecordData()
        val itemData2 = getAccountRecordData2()
        mAccountInRecordGroupAdapter.setList(itemData)
        mAccountInRecordGroupAdapter2.setList(itemData2)
//        val itemDecoration = GroupedLinearItemDecoration(
//            mAccountInRecordGroupAdapter,
//            1,
//            null,
//            1,
//            null,
//            1,
//            ContextCompat.getDrawable(mActivity,R.drawable.divider_common_line)
//        )
//        vb.rvRecordList.addItemDecoration(DividerItemUtils.dividerItemDecorationTool(
//            mActivity,
//            drawId = R.drawable.divider_common_line
//        )//自定义间隔线)
//        )
        vb.rvRecordList.adapter = mAccountInRecordGroupAdapter2
        vb.refreshLayout.setEnableRefresh(true)
        vb.refreshLayout.setEnableLoadMore(true)
        vb.refreshLayout.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onRefresh(refreshLayout: RefreshLayout) {
                pageNo = 1
                mAccountInRecordGroupAdapter2.setList(getAccountRecordData2())
                //发送请求获取第一页的数据
                refreshLayout.finishRefresh(1000)
                if (mAccountInRecordGroupAdapter2.hasFooterLayout()) {
                    mAccountInRecordGroupAdapter2.removeFooterView(noDataView)
                    return
                }
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                pageNo++
                //发送请求获取第一页的数据
                refreshLayout.finishLoadMore(1000)
                mAccountInRecordGroupAdapter2.addData(getAccountRecordData2())
                if (mAccountInRecordGroupAdapter2.hasFooterLayout()) {
                    return
                }
                mAccountInRecordGroupAdapter2.addFooterView(noDataView)

                //获取一个组的组尾下标 如果该组没有组尾 返回-1
//                val index = mAccountInRecordGroupAdapter.getPositionForGroupFooter(mAccountInRecordGroupAdapter.groupCount-1)
//                mAccountInRecordGroupAdapter.getData()[index].footerView = true
//                mAccountInRecordGroupAdapter.notifyFooterChanged(index)
            }
        })
        mAccountInRecordGroupAdapter2.setOnItemClickListener{adapter, view, position ->
            val item = adapter.data[position] as AccountInRecordInfo2
            if (item.isHeader)
            {
                showMsg("你点击的是头部")
            }
            else
            {
                showMsg(item.childItem.toString())
                startActivity<AccountInDetailAct>()
            }
        }
    }

    override fun initData() {
        super.initData()

    }

    fun getAccountRecordData(): List<AccountInRecordInfo> {

        val itemList = mutableListOf<AccountInRecordInfo>()
        for (i in 0 until 2) {
            val item = AccountInRecordInfo("2022-08-0$i", "合计 ¥47.00", "2笔")
            val childList = mutableListOf<AccountInRecordInfo.ChildItem>()
            for (j in 0 until 3) {
                val child = AccountInRecordInfo.ChildItem("首笔激活活动$j", "16:13:34", "¥36.50")
                childList.add(child)
            }
            item.childItemList = childList
            itemList.add(item)
        }
        return itemList
    }

    fun getAccountRecordData2(): List<AccountInRecordInfo2> {

        val itemList = mutableListOf<AccountInRecordInfo2>()
        for (i in 0 until 2) {
            val item = AccountInRecordInfo2("2022-08-0$i", "合计 ¥47.00", "2笔")
            item.isHeader = true
            itemList.add(item)
//            val childList = mutableListOf<AccountInRecordInfo.ChildItem>()
            for (j in 0 until 3) {
                val childItem = AccountInRecordInfo2()
                childItem.isHeader = false
                val child = AccountInRecordInfo.ChildItem("首笔激活活动$j", "16:13:34", "¥36.50")
                childItem.childItem = child
                itemList.add(childItem)
            }

        }
        return itemList
    }


    override fun lazyInit() {

    }
}